import { Injectable, Inject } from '@nestjs/common';
import { Queue } from 'bullmq';
import { InjectQueue } from '@nestjs/bullmq';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'
import { OrderCreatedEvent } from './emitter/Emitter';

@Injectable()
export class AppService {
  constructor(
    @InjectQueue('audio')
    private audioQueue: Queue,
    @Inject(EventEmitter2)
    private eventEmitter: EventEmitter2,
  ) { }

  // 测试调用 server 层
  getHello(): string {
    return 'Hello World!';
  }

  // 测试 bullmq 添加数据
  async addValToBullMq(value: string) {
    const job = await this.audioQueue.add('addValToBullMq', {
      name: value,
    });
  }

  // 测试 event-emitter
  async triggerEmitter(orderId: number) {
    this.eventEmitter.emit(
      'order.created',
      new OrderCreatedEvent({
        orderId,
        payload: {},
      }),
    );
  }

  @OnEvent('order.created', { async: true })
  handleOrderCreatedEvent(payload: OrderCreatedEvent) {
    // handle and process "OrderCreatedEvent" event
    console.log('order.created :>> ', payload)
  }
}
